package org.lq.er.view.admission;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.lq.er.entity.Exam_information;
import org.lq.er.entity.Exam_position;
import org.lq.er.entity.Student;
import org.lq.er.entity.Subject;
import org.lq.er.service.impl.Exam_informationServiceImpl;
import org.lq.er.service.impl.Exam_positionServiceImpl;
import org.lq.er.service.impl.SubjectServiceImpl;
import org.lq.er.util.DateUtil;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 下载准考证
 * @author 张鹤山
 *
 */
public class Admission {


	Student student;
	Exam_information exam_information;
	Subject subject;
	Exam_position exam_position;
	SubjectServiceImpl ssi = new SubjectServiceImpl();
	Exam_informationServiceImpl eis = new Exam_informationServiceImpl();
	Exam_positionServiceImpl eps = new Exam_positionServiceImpl();

	public Admission(){
	}

	public Admission(Student stu) {
		this();
		this.student = stu;
		choice();
	}

	/**
	 * 选择路径
	 */
	public void choice() {
		String path="";
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = chooser.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			path = chooser.getSelectedFile().getAbsolutePath();
		}
		init(path);
	}

	/**
	 * 初始化pdf文档
	 */
	public void init(String path){
		exam_information = eis.getByStu_Id(student.getCard_id());
		subject = ssi.findById(exam_information.getSubject_id());
		exam_position = eps.getById(exam_information.getExam_position_id());

		String name = subject.getSubjectName().equals("CET_4")? "全国大学英语四级考试": "全国大学英语六级考试";
		Document document = new Document(); 
		try {
			//建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
			PdfWriter.getInstance(document, new FileOutputStream(path+"\\"+student.getName()+".PDF"));
			//打开文档。
			document.open(); 
			//向文档中添加内容。
			BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font headFont=new Font(bfChinese,25,Font.BOLD);
			Font keyFont=new Font(bfChinese,15,Font.BOLD);
			Font textFont=new Font(bfChinese,10,Font.NORMAL);
			Font tipFont=new Font(bfChinese,8,Font.NORMAL);
			Paragraph title =  new Paragraph(name+"\n", headFont);
			Chunk c_admission = new Chunk("准考证", keyFont);
			title.add(c_admission);
			title.setAlignment(1);
			document.add(title); 

			//加入空行
			document.add(new Paragraph(18f, " ", textFont));

			PdfPTable table1 = new PdfPTable(1);
			PdfPCell cell11 = new PdfPCell(new Paragraph("身份信息", keyFont));
			cell11.setMinimumHeight(20);
			cell11.setBorder(0);
			table1.addCell(cell11);  
			document.add(table1);

			PdfPTable table2 = new PdfPTable(3);
			int width2[] = {40, 30, 40};
			table2.setWidths(width2); 
			//            table2.getDefaultCell().setBorder(0);
			PdfPCell cell21 = new PdfPCell(new Paragraph("学号： "+student.getStu_no(), textFont));
			PdfPCell cell22 = new PdfPCell(new Paragraph("姓名： "+student.getName(), textFont));
			PdfPCell cell23 = new PdfPCell(new Paragraph("手机号："+student.getPhone_no(), textFont));
			//            cell21.setBorder(0);
			table2.addCell(cell21); 
			table2.addCell(cell22);
			table2.addCell(cell23);
			document.add(table2);

			PdfPTable table3 = new PdfPTable(2);
			int width3[] = {50, 50};
			table3.setWidths(width3); 
			//            table2.getDefaultCell().setBorder(0);
			PdfPCell cell31 = new PdfPCell(new Paragraph("证件类型： 中华人民共和国居民身份证", textFont));
			PdfPCell cell32 = new PdfPCell(new Paragraph("证件号码： "+student.getCard_id(), textFont));
			//            cell21.setBorder(0);
			table3.addCell(cell31); 
			table3.addCell(cell32);
			document.add(table3);

			PdfPTable table4 = new PdfPTable(3);
			int width4[] = {40, 35, 25};
			table4.setWidths(width4); 
			//            table2.getDefaultCell().setBorder(0);
			PdfPCell cell41 = new PdfPCell(new Paragraph("所属学校： 河南科技学院", textFont));
			PdfPCell cell42 = new PdfPCell(new Paragraph("院系： 信息工程系", textFont));
			PdfPCell cell43 = new PdfPCell(new Paragraph("班级："+student.getClasses(), textFont));
			//            cell21.setBorder(0);
			table4.addCell(cell41); 
			table4.addCell(cell42);
			table4.addCell(cell43);
			document.add(table4);

			document.add(new Paragraph(18f, " ", textFont));

			PdfPTable table5 = new PdfPTable(1);
			PdfPCell cell51 = new PdfPCell(new Paragraph("时间安排", keyFont));
			cell51.setMinimumHeight(20);
			cell51.setBorder(0);
			table5.addCell(cell51);  
			document.add(table5);

			PdfPTable table6 = new PdfPTable(5);
			int width6[] = {25, 15, 20, 15, 25};
			table6.setWidths(width6); 
			PdfPCell cell61 = new PdfPCell(new Paragraph("考试", textFont));
			PdfPCell cell62 = new PdfPCell(new Paragraph("场次", textFont));
			PdfPCell cell63 = new PdfPCell(new Paragraph("考试日期", textFont));
			PdfPCell cell64 = new PdfPCell(new Paragraph("报道时间", textFont));
			PdfPCell cell65 = new PdfPCell(new Paragraph("考试时间", textFont));
			cell61.setBorder(3);
			cell62.setBorder(3);
			cell63.setBorder(3);
			cell64.setBorder(3);
			cell65.setBorder(3);
			cell61.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell62.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell63.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell64.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell65.setHorizontalAlignment(Element.ALIGN_CENTER);
			table6.addCell(cell61); 
			table6.addCell(cell62);
			table6.addCell(cell63);
			table6.addCell(cell64);
			table6.addCell(cell65);
			document.add(table6);

			PdfPTable table7 = new PdfPTable(5);
			int width7[] = {25, 15, 20, 15, 25};
			table7.setWidths(width7); 
			PdfPCell cell71 = new PdfPCell(new Paragraph(subject.getSubjectName(), textFont));
			PdfPCell cell72 = new PdfPCell(new Paragraph("--", textFont));
			PdfPCell cell73 = new PdfPCell(new Paragraph(DateUtil.toDate(subject.getBeginTime()), textFont));
			PdfPCell cell74 = new PdfPCell(new Paragraph(DateUtil.timeOperation(subject.getBeginTime(), -30), textFont));
			PdfPCell cell75 = new PdfPCell(new Paragraph(DateUtil.toTime(subject.getBeginTime())+"-"+DateUtil.timeOperation(subject.getBeginTime(), subject.getDuration()), textFont));
			cell71.setBorder(3);
			cell72.setBorder(3);
			cell73.setBorder(3);
			cell74.setBorder(3);
			cell75.setBorder(3);
			cell71.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell72.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell73.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell74.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell75.setHorizontalAlignment(Element.ALIGN_CENTER);
			table7.addCell(cell71); 
			table7.addCell(cell72);
			table7.addCell(cell73);
			table7.addCell(cell74);
			table7.addCell(cell75);
			document.add(table7);

			document.add(new Paragraph(18f, " ", textFont));

			PdfPTable table8 = new PdfPTable(1);
			PdfPCell cell81 = new PdfPCell(new Paragraph("考场安排", keyFont));
			cell81.setMinimumHeight(20);
			cell81.setBorder(0);
			table8.addCell(cell81);  
			document.add(table8);

			PdfPTable table9 = new PdfPTable(4);
			int width9[] = {25, 45, 15, 15};
			table9.setWidths(width9); 
			PdfPCell cell91 = new PdfPCell(new Paragraph("考试", textFont));
			PdfPCell cell92 = new PdfPCell(new Paragraph("考试地点", textFont));
			PdfPCell cell93 = new PdfPCell(new Paragraph("考场号", textFont));
			PdfPCell cell94 = new PdfPCell(new Paragraph("座位号", textFont));
			cell91.setBorder(3);
			cell92.setBorder(3);
			cell93.setBorder(3);
			cell94.setBorder(3);
			cell91.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell92.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell93.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell94.setHorizontalAlignment(Element.ALIGN_CENTER);
			table9.addCell(cell91); 
			table9.addCell(cell92);
			table9.addCell(cell93);
			table9.addCell(cell94);
			document.add(table9);

			PdfPTable table = new PdfPTable(4);
			int width[] = {25, 45, 15, 15};
			table.setWidths(width); 
			PdfPCell cell1 = new PdfPCell(new Paragraph(subject.getSubjectName(), textFont));
			PdfPCell cell2 = new PdfPCell(new Paragraph(exam_position.getPosition(), textFont));
			PdfPCell cell3 = new PdfPCell(new Paragraph(String.valueOf(exam_information.getExam_position_id()), textFont));
			PdfPCell cell4 = new PdfPCell(new Paragraph(String.valueOf(exam_information.getSeat_num()), textFont));
			cell1.setBorder(3);
			cell2.setBorder(3);
			cell3.setBorder(3);
			cell4.setBorder(3);
			cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell1); 
			table.addCell(cell2);
			table.addCell(cell3);
			table.addCell(cell4);
			document.add(table);

			document.add(new Paragraph(18f, " ", textFont));

			PdfPTable table_information = new PdfPTable(1);
			PdfPCell table_information1 = new PdfPCell(new Paragraph("考生须知", keyFont));
			table_information1.setMinimumHeight(20);
			table_information.addCell(table_information1);  
			document.add(table_information);
			PdfPTable text_information = new PdfPTable(1);
			PdfPCell cell_information = new PdfPCell();
			cell_information.addElement(new Paragraph("1.考试当日考生须携带相应科目准考证、报考时所使用的身份证件及学校规定的其他证件按规定时间到达准考证上指定的考场，"
					+ "配合监考员完成身份核对，并在考场签到表上相应位置签字，并按考点要求将与考试无关物品放置在指定位置。证件携带不齐全或不配合监考| 员完成身份核对、"
					+ "签到及拒绝将与考试无关物品放置在指定位置的考生将不得进入考场，情节严重的将按违规处理。\n", tipFont));
			cell_information.addElement(new Paragraph("2.考生进入考场后须按准考证上的位置入座，否则按违规处理。\n", tipFont));
			cell_information.addElement(new Paragraph("3.考生须听从监考员指令，在规定时间打开试卷、作答和停止作答，否则按违规处理。\n", tipFont));
			cell_information.addElement(new Paragraph("4.考生在答题前，请认真完成以下内容:(1)请检查试题册背面条形码粘贴条、答题卡的印刷质量，如有问题及时向监考员反映，"
					+ "确认无误后完成以下两点要求: (2)请将试题册背面条形码粘贴条揭下后粘贴在答题卡1的条形码粘贴框内，并将姓名和准考证号填写在试题册背面相应位置: "
					+ "(3)请在答题卡1和答题卡2指定位置用黑色签字笔填写准考证号、姓名和学校名称，并用HB-2B铅笔将对应准考证号的信息点涂黑。\n", tipFont));
			cell_information.addElement(new Paragraph("5.考生在考试过程中，请注意以下内容:(1)所有题目必须在答题卡上规定位置作答，在试题册上或答题卡上非规定位置的作答一一律无效:"
					+ " (2)请在规定时间内在各题卡指定位置依次完成作文、听力、阅读、翻译各部分考试，作答作文期间不得翻阅该试题册。听力录音播放完毕后，请立即停止作答，"
					+ "监考员将立即回收答题卡,得到监考员指令后方可继续作答:(3)作文题内容印在试题册背面，作文题及其他主观题必须用黑色签字笔在答题卡指定区域内作答。\n", tipFont));
			cell_information.addElement(new Paragraph("6.考生在考试过程中出现以下情况按违规处理:(1)不正确填写(涂)个人信息，错贴、不贴、毁损条形码粘贴条: "
					+ "(2)未按规定翻阅试题册、提前阅读试题、提前或在收答题卡期间作答: (3)未用所规定的笔作答、折叠或毁损答题卡导致无法评卷: (4)考试期间在非听力考试时间佩戴耳机: "
					+ "(5)CET相关规定中规定的违规行为。\n", tipFont));
			cell_information.addElement(new Paragraph("7.在考试过程中出现突发事件导致考试无法正常进行时，考生须听从监考员安排，相关情况未解决之前，不得离开考场。\n", tipFont));
			cell_information.addElement(new Paragraph("8.以上内容中有关条形码及题目作答顺序等内容仅针对英语四级和英语六级笔试科目。\n", tipFont));
			cell_information.addElement(new Paragraph("9.准考证号是成绩查询的重要依据，请妥善保管。\n", tipFont));
			text_information.addCell(cell_information);
			document.add(text_information);

			document.add(new Paragraph(18f, " ", textFont));

			PdfPTable tip = new PdfPTable(1);
			PdfPCell tip1 = new PdfPCell(new Paragraph("考点提示", keyFont));
			tip1.setMinimumHeight(20);
			tip.addCell(tip1);  
			document.add(tip);
			PdfPTable tip_text = new PdfPTable(1);
			PdfPCell cell_tip = new PdfPCell(new Paragraph("考生注意事项: 1.凭本人CET准考证、身份证和学校一卡通(准考证)参加考试，三证缺一不可。必须按规定时间(上午8: 45、 下午14:45)入场，入场开始15分钟(即上午9: 00、下午15: 00)后，"
					+ "禁止入场。入场后将三证放在课桌上，接受监考人员核验，并按要求在考场座次表上签子。2.只准携带铅笔、黑色签字笔、橡皮等必要的文具入场，禁止携带手机、电子辞典、寻呼机、手表等辅助工具和物品。"
					+ "考场内不得擅自相互借用文具。在非听力考试时间，必须摘下耳机。3.书写部分一律用黑色签字笔作答，填涂信息点时只能用2B铅笔涂器:只能在规定考生作答的位置书写或填涂信息点，不按规定要求填涂和作答，一律无效。"
					+ "错贴、不贴条形码粘贴条按违规处理。4.遇试卷分发错误或试题字迹不清等情况应及时要求更换:涉及试题内容的疑问，不得向监考员询问。5.在考场内必须严格遵守考场纪律，对于违反考场规定和不服从考试工作人员管理者，"
					+ "取消考试成绩并按校纪校规处理。对扰乱考场秩序，恐吓、威胁考试工作人员的将移交公安机关追究其责任，参加四、六级考试违纪作弊的学生一年内不允许报名参加考试，直接取消下次报名资格: 6.除有特殊原因，在考试结束"
					+ "前禁止提前退场。考试结束铃声响起时，要立即停正答题，将答卷扣放在桌面上，等监考员允许后方可离开考场。离开考场时必须交卷，不准携带试卷、答题卡离开考场。7.通过四、六级考试网上报名，确认参加考试但无故缺考"
					+ "的学生，取消下一次报名资格。", tipFont));
			tip_text.addCell(cell_tip);
			document.add(tip_text);

			//关闭文档。
			document.close(); 
			
			//成功提示
			JOptionPane.showMessageDialog(null, "下载成功","提示",JOptionPane.INFORMATION_MESSAGE);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 

	}

//	public static void main(String[] args) {
//				StudentServiceImpl ss = new StudentServiceImpl();
//				new Admission(ss.getById(1004));
//	}
}
